// AirsoftTech.dk //////// WIRE PIN AN COMPONENT LAYOUT ///////// int MOSFET_PIN = 5; // The Digital pin that attaches to the MOSFET gate, to turn it on and off. int TRIGGER_PIN = A0; // The analog pin that attaches to the trigger pin float R1 = 100; // 100Kohm // The Voltage devider resistor R1 float R2 = 10; // 100Kohm // The Voltage devider resistor R2 //////// CONFIG VALUES ///////// int Max_ON_Time = 200; // The time in MS for a full burst cycle... //////// INTERNAL VALUES ///////// int TrigerStatus = LOW; // The state of the trigger LOW => Not pressed, HIGH => Pressed int TriggerReadValue = 0; // The value read from the analog trigger pin. => 0-1024 int CurrentSleepTime = 0; // The ammount of time the mosfet has been on. // The setup routine runs once when you press reset. void setup() { // Initialize the digital pin as an output. pinMode(13, OUTPUT); // LED pin pinMode(MOSFET_PIN, OUTPUT); // Set the Mosfet pin as an output so that we can send power to the mosfet. digitalWrite(MOSFET_PIN, LOW); // Make sure we start with power OFF! digitalWrite(13, LOW); // Make sure we start with power OFF! } // The loop routine runs over and over again forever. void loop() { ReadTrigger(); // Update the trigger state if(TrigerStatus == HIGH) // If the trigger is pushed. { SetMosfet(HIGH); // First turn the Mosfet On while (TrigerStatus == HIGH) { if(CurrentSleepTime < Max_ON_Time){ // If we have not completed a cycle CurrentSleepTime = CurrentSleepTime + 1; // Leave the mosfet on, and increase time counter. } else { SetMosfet(LOW); // If a full cycle has gone, turn the Monfet Off. } delay(1); // Sleep for one milisecond ReadTrigger(); // Update the trigger status (We stay in the loop untill the trigger is released) } SetMosfet(LOW); // Make sure the Mosfet is off when the trigger is released CurrentSleepTime = 0; // Reset time counter when the trigger is released. } } // This function updates the trigger status when it's called. void ReadTrigger() { int TriggerReadValue = analogRead(TRIGGER_PIN); TrigerStatus = LOW; if(TriggerReadValue > 20) { TrigerStatus = HIGH; } } // This function set the Mosfet state void SetMosfet(int val) { digitalWrite(13, val); // turn the LED on / off to indicate what the mosfet should be dooing digitalWrite(MOSFET_PIN, val); // turn the Monfet on / off }